package com.swordget.service.impl;

import com.swordget.commons.ColumnType;
import com.swordget.entity.ColumnDesign;
import com.swordget.exception.VerifyFailException;
import com.swordget.service.VerifyColumnService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

import static com.swordget.commons.ColumnType.NUMBER;
import static com.swordget.commons.ColumnType.VARCHAR2;

@Service
public class VerifyColumnServiceImpl implements VerifyColumnService {

    /**
     * 支持的字段类型
     */
    private Set columnTypeSet = new HashSet(Arrays.asList(new String[]{"VARCHAR", "NUMBER"}));

    /**
     * 用于注释校验
     * ^\w{1,20}$|^[\u4e00-\u9fa5,\w]{1,20}$
     */
    private final static Pattern commentCheck = Pattern.compile("^[a-zA-Z0-9\\u4E00-\\u9FA5\\uf900-\\ufa2d_]{1,20}$");

    /**
     * 支持的日期类型
     */
    private Set dateFormatSet = new HashSet(Arrays.asList(new String[]{"yyyy-MM-dd", "yyyy/MM/dd", "yyyy年MM月dd日",
            "yy-MM-dd", "yy/MM/dd", "yy年MM月dd日", "yyyy-MM-dd HH:mm:ss"}));

    /**
     * 字段名正则，用于校验字段名
     */
    private final static Pattern columnCheck = Pattern.compile("^\\w{1,30}$");

    @Override
    public void verifyColumn(List<ColumnDesign> columns) {
        for (ColumnDesign column : columns) {
            String name = column.getName().toUpperCase();
            column.setName(name);
            String type = column.getType();
            int width = column.getWidth();
            int point = column.getPoint();
            String notNull = column.getNotNull();
            String comment = column.getComment();
            //是否是搜索条件 N 不是 Y 是
            String search = column.getSearch();
            String contentType = column.getContextType();
            String dateFormat = column.getDateFormat();
            //下拉框url
            String comboUrl = column.getComboUrl();
            //必填项判断
            if (StringUtils.isAnyBlank(name, type, notNull, comment, search, contentType)) {
                throw new VerifyFailException("字段-" + comment + "请输入必填项!");
            }
            //对字段名和字段注释做正则校验
            if (!columnCheck.matcher(name).find() || !commentCheck.matcher(comment).find()) {
                throw new VerifyFailException("字段-" + comment + "名称、注释格式不正确!");
            }
            //下拉框必须填写下拉框地址
            if ("3".equals(contentType) && StringUtils.isBlank(comboUrl)) {
                throw new VerifyFailException("字段-" + comment + "必须填写下拉框值且需用-分割!");
            }
            if ("3".equals(contentType) && !comboUrl.contains("-")) {
                throw new VerifyFailException("字段-" + comment + "下拉框值必须用'-'分割!");
            }
            /*if ("3".equals(contentType) && StringUtils.isBlank(comboUrl)) {
                throw new VerifyFailException("字段-" + comment + "必须填写下拉框地址api!");
            }*/
            //日期框格式校验
            if ("2".equals(contentType)) {
                if (StringUtils.isBlank(dateFormat)) {
                    throw new VerifyFailException("字段-" + comment + "必须选择日期格式!");
                }
                if (!dateFormatSet.contains(dateFormat)) {
                    throw new VerifyFailException("字段-" + comment + "应用程式不支持此种日期格式!");
                }
                try {
                    new SimpleDateFormat(dateFormat).format(new Date());
                } catch (IllegalArgumentException e) {
                    throw new VerifyFailException("字段-" + comment + "无法解析这种日期格式!");
                }
            }
            //对字段类型做校验
            if (!columnTypeSet.contains(type)) {
                throw new VerifyFailException("字段-" + comment + "类型不正确!");
            }
            //对字段长度做校验，对于varchar2最大长度4000,对于number最大长度小数位和整数位共不超过38位，小数最大7位
            Integer columnWidth = width;
            Integer columnPoint = point;
            if (VARCHAR2.equals(type)) {
                if (columnWidth <= 0 || columnWidth > 4000) {
                    throw new VerifyFailException("字段-" + comment + "长度不正确!");
                }
            }
            if (NUMBER.equals(type)) {
                if (columnWidth <= 0 || (columnWidth + columnPoint) > 38 || columnPoint > 37 || columnPoint >= columnWidth) {
                    throw new VerifyFailException("字段-" + comment + "比例不正确!");
                }
            }
        }
    }

    @Override
    public void verifyColumnByUpdate(ColumnDesign oldCol, ColumnDesign newCol) {
        //暂不支持字符串转数值(修改前是字符型，修改后值数值型)
        if (ColumnType.VARCHAR2.equals(oldCol.getType()) && ColumnType.NUMBER.equals(newCol.getType())) {
            throw new RuntimeException("不支持的修改," + oldCol.getType() + "--->" + newCol.getType());
        }
       /* if (newCol.getWidth() < oldCol.getWidth()) {
            throw new RuntimeException("类型大小不能减小");
        }
        if (ColumnType.NUMBER.equals(newCol.getType())) {
            if ((newCol.getWidth() - newCol.getPoint()) < (oldCol.getWidth() - oldCol.getPoint())) {
                throw new RuntimeException("类型大小和比例有误，请检查");
            }
        }*/
    }
}